﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Data.SqlClient;


namespace HotelManagement
{
    public partial class NewGust : Form
    {
        SqlConnection Cnn = new SqlConnection(Globals.DBConnection());
        DataSet ds = new DataSet();
        SqlDataAdapter da;
        int c_id;

        public NewGust()
        {
            InitializeComponent();
        }

        private void NewGust_Load(object sender, EventArgs e)
        {
            toolStripUsername.Text = "نام کاربر : " + Globals.GetUserName();
            PersianCalendar PC = new PersianCalendar();
            string shamsidate =
                PC.GetYear(DateTime.Now).ToString() + "/" +
                PC.GetMonth(DateTime.Now).ToString() + "/" +
                PC.GetDayOfMonth(DateTime.Now).ToString();

            toolStripDate.Text = shamsidate;
            toolStripTime.Text = DateTime.Now.ToLongTimeString();

            da = new SqlDataAdapter("SELECT c_id AS [شماره مهمان] ,c_fname as [نام],c_lname as [نام خانوادگی] ,c_fathername as[نام پدر] ,c_dateofbirth as [تاریخ تولد],c_nationality as [ملیت],c_ncode as [شماره ملی] ,c_address as [آدرس],c_phonenumber as [شماره تماس]  from Customer order by c_id desc ", Cnn);
            da.Fill(ds);
            dGView_Customer.DataSource = ds.Tables[0];
        }

        private void BtnCustomerSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(fnameTxt.Text) && !string.IsNullOrEmpty(lnameTxt.Text) && !string.IsNullOrEmpty(ncodeTxt.Text) && !string.IsNullOrEmpty(phonenumTxt.Text) && !string.IsNullOrEmpty(fathernameTxt.Text) && !string.IsNullOrEmpty(NatianalityTxt.Text) && !string.IsNullOrEmpty(AddressTxt.Text))
            {
                SqlCommand SelectCustomer = new SqlCommand("SELECT c_ncode FROM Customer WHERE c_ncode=@ncode", Cnn);
                SelectCustomer.Parameters.AddWithValue("@ncode", ncodeTxt.Text.Trim());
                Cnn.Open();
                SqlDataReader dr = SelectCustomer.ExecuteReader();
                bool hasrows = dr.HasRows;
                Cnn.Close();

                if (hasrows)
                {
                    MessageBox.Show("این کد ملی قبلا در سیستم ثبت شده است");
                }

                else
                {
                    SqlCommand SaveINDB = new SqlCommand("INSERT INTO Customer (c_fname,c_lname,c_fathername,c_dateofbirth,c_nationality,c_ncode,c_address,c_phonenumber) values (@fname,@lname,@fathername,@dateofbirth,@nationality,@ncode,@address,@phonenumber)", Cnn);
                    SaveINDB.Parameters.AddWithValue("@fname", fnameTxt.Text.Trim());
                    SaveINDB.Parameters.AddWithValue("@lname", lnameTxt.Text.Trim());
                    SaveINDB.Parameters.AddWithValue("@fathername", fathernameTxt.Text.Trim());
                    SaveINDB.Parameters.AddWithValue("@dateofbirth", BirthDateTxt.Text.Trim());
                    SaveINDB.Parameters.AddWithValue("@nationality", NatianalityTxt.Text.Trim());
                    SaveINDB.Parameters.AddWithValue("@address", AddressTxt.Text.Trim());
                    SaveINDB.Parameters.AddWithValue("@ncode", ncodeTxt.Text.Trim());
                    SaveINDB.Parameters.AddWithValue("@phonenumber", phonenumTxt.Text.Trim());

                    Cnn.Open();
                    SaveINDB.ExecuteNonQuery();
                    Cnn.Close();

                    ds.Clear();
                    da.Fill(ds);
                    dGView_Customer.DataSource = ds.Tables[0];

                    if (DialogResult.Yes == MessageBox.Show("آیا میخواهید برای این مهمان هم اکنون رزرو اتاق را انجام دهید", "پذیرش", MessageBoxButtons.YesNo))
                    {
                        ReserveFrm rsrfrm = new ReserveFrm();
                        rsrfrm.FromCustomerFRM = true;
                        rsrfrm.Show();
                    }

                }
            }

            else
            {
                MessageBox.Show("لطفا همه قسمت ها را به درستی تکمیل نمایید");
            }

        }

        private void BtnCustomerDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("آیا برای حذف مهمان مطمئن هستید؟", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                SqlCommand DeleteCMD = new SqlCommand("DELETE FROM Customer WHERE c_id=@c_id", Cnn);
                DeleteCMD.Parameters.AddWithValue("@c_id", c_id);
                Cnn.Open();
                try
                {
                    DeleteCMD.ExecuteNonQuery();
                }
                catch (SqlException)
                {
                    MessageBox.Show("این مشتری در حال اقامت است", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Cnn.Close();
                    return;
                }
                finally
                {
                    Cnn.Close();
                }
                ds.Clear();
                da.Fill(ds);
                dGView_Customer.DataSource = ds.Tables[0];

                BtnCustomerDelete.Enabled = false;
                BtnCustomerEdit.Enabled = false;
                fnameTxt.Text = "";
                lnameTxt.Text = "";
                ncodeTxt.Text = "";
                phonenumTxt.Text = "";
                AddressTxt.Text = "";
                NatianalityTxt.Text = "";
                fathernameTxt.Text = "";

                MessageBox.Show("حذف مهمان با موفقیت انجام شد");
   
            }
        }

        private void BtnCustomerEdit_Click(object sender, EventArgs e)
        {
            SqlCommand UpdateCMD = new SqlCommand("UPDATE Customer SET c_fname=@fname, c_lname=@lname ,c_fathername=@fathername,c_dateofbirth=@dateofbirth,c_nationality=@nationality,c_address=@address, c_ncode=@ncode , c_phonenumber=@phonenumber WHERE c_id=@c_id", Cnn);

            UpdateCMD.Parameters.AddWithValue("@c_id", c_id);
            UpdateCMD.Parameters.AddWithValue("@fname", fnameTxt.Text.Trim());
            UpdateCMD.Parameters.AddWithValue("@lname", lnameTxt.Text.Trim());
            UpdateCMD.Parameters.AddWithValue("@fathername", fathernameTxt.Text.Trim());
            UpdateCMD.Parameters.AddWithValue("@dateofbirth", BirthDateTxt.Text.Trim());
            UpdateCMD.Parameters.AddWithValue("@nationality", NatianalityTxt.Text.Trim());
            UpdateCMD.Parameters.AddWithValue("@address", AddressTxt.Text.Trim());
            UpdateCMD.Parameters.AddWithValue("@ncode", ncodeTxt.Text.Trim());
            UpdateCMD.Parameters.AddWithValue("@phonenumber", phonenumTxt.Text.Trim());


            Cnn.Open();
            UpdateCMD.ExecuteNonQuery();
            Cnn.Close();

            ds.Clear();
            da.Fill(ds);
            dGView_Customer.DataSource = ds.Tables[0];


            BtnCustomerDelete.Enabled = false;
            BtnCustomerEdit.Enabled = false;
            fnameTxt.Text = "";
            lnameTxt.Text = "";
            ncodeTxt.Text = "";
            phonenumTxt.Text = "";
            AddressTxt.Text = "";
            NatianalityTxt.Text = "";
            fathernameTxt.Text = "";


            MessageBox.Show("تغییر مشخصات با موفقیت انجام شد");
        }

        private void dGView_Customer_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            fnameTxt.Text = dGView_Customer.CurrentRow.Cells[1].Value.ToString().Trim();
            lnameTxt.Text = dGView_Customer.CurrentRow.Cells[2].Value.ToString().Trim();
            fathernameTxt.Text = dGView_Customer.CurrentRow.Cells[3].Value.ToString().Trim();

            NatianalityTxt.Text = dGView_Customer.CurrentRow.Cells[5].Value.ToString().Trim();
            ncodeTxt.Text = dGView_Customer.CurrentRow.Cells[6].Value.ToString().Trim();
            AddressTxt.Text = dGView_Customer.CurrentRow.Cells[7].Value.ToString().Trim();
            phonenumTxt.Text = dGView_Customer.CurrentRow.Cells[8].Value.ToString().Trim();

            c_id = (int)dGView_Customer.CurrentRow.Cells[0].Value;
            BtnCustomerDelete.Enabled = true;
            BtnCustomerEdit.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripTime.Text = DateTime.Now.ToLongTimeString();
        }

        private void fNameSearchTxt_TextChanged(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("SELECT c_id AS [شماره مهمان] ,c_fname as [نام],c_lname as [نام خانوادگی] ,c_fathername as[نام پدر] ,c_dateofbirth as [تاریخ تولد],c_nationality as [ملیت],c_ncode as [شماره ملی] ,c_address as [آدرس],c_phonenumber as [شماره تماس]  from Customer WHERE (c_fname LIKE @fname + N'%' OR @fname=N'') AND (c_lname LIKE @lname + N'%' OR @lname=N'') AND (c_ncode LIKE @ncode + N'%' OR @ncode=N'') order by c_id desc ", Cnn);
            da.SelectCommand.Parameters.AddWithValue("@fname", fNameSearchTxt.Text);
            da.SelectCommand.Parameters.AddWithValue("@lname", LnameSearchTxt.Text);
            da.SelectCommand.Parameters.AddWithValue("@ncode", NcodeSearchTxt.Text);

            ds.Clear();
            da.Fill(ds);
            dGView_Customer.DataSource = ds.Tables[0];
        }

        private void BirthDateBtn_Click(object sender, EventArgs e)
        {
            DayPicker form = new DayPicker();
            form.ShowDialog();

            PersianCalendar PC = new PersianCalendar();
            DateTime sdate = new DateTime(form.getyear(), form.getmonth(), form.getday());
            string shamsidate =
                PC.GetYear(sdate).ToString() + "/" +
                PC.GetMonth(sdate).ToString() + "/" +
                PC.GetDayOfMonth(sdate).ToString();

            BirthDateTxt.Text = shamsidate;
        }

    }
}